<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/3/28 20:45
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

class Collect extends WX_Controller
{

    /**
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function getError()
    {
        return parent::getError();
    }

    /**
     * 收藏列表
     */
    public function lists()
    {
        if (!empty($this->openId)) {
            $this->load->model('wx/Collect_Model', 'collect');
            $res = $this->collect->getCollectGoods($this->userid);
            if (!$res) {
                $this->output(['total' => 0, 'data' => 0]);
            } else {
                $this->output($res);
            }
        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }


    public function deleteItem()
    {
        if (!empty($this->openId)) {

            $goodsId = $this->input->post('goodsId');
            $this->load->model('wx/Collect_Model', 'collect');
            if ($this->collect->deleteItem($this->userid, $goodsId)) {
                $this->output('移除成功');
            } else {
                $this->outputError('移除失败');
            }
        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }

    /**
     * 收藏、取消收藏
     */
    public function doCollect()
    {
        if (!empty($this->openId)) {

            $goodsId = $this->input->post('goodsId');
            $action = $this->input->post('action');

            if (empty($goodsId) || !in_array($action, ['on', 'off'])) {
                $this->outputError("参数错误");
            }

            $this->load->model('wx/Collect_Model', 'collect');
            if (!$this->collect->doCollect($this->userid, $goodsId, $action)) {
                $this->outputError('操作失败');
            }
            $this->output('成功');
        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }

}
